﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ApqAlarm
{
	/// <summary>
	/// 通过 中国短信中心 发短信
	/// </summary>
	public class c123
	{
		protected static string _uid
		{
			get { return GlobalObject.XmlConfigChain[typeof(c123), "uid"]; }
		}
		protected static string _pwdMD5
		{
			get
			{
				string strPwd = GlobalObject.XmlConfigChain[typeof(c123), "pwd"];
				string strPwdMD5 = Apq.Security.Cryptography.MD5Helper.EncryptString(strPwd);
				return strPwdMD5;
			}
		}
		public static string Url
		{
			get { return GlobalObject.XmlConfigChain[typeof(c123), "Url"]; }
		}

		public static bool Send(string Mobiles, string Content)
		{
			bool rtn = false;
			StringBuilder sbTemp = new StringBuilder();
			sbTemp.Append("uid=");
			sbTemp.Append(_uid);
			sbTemp.Append("&pwd=");
			sbTemp.Append(_pwdMD5);
			sbTemp.Append("&mobile=");
			sbTemp.Append(Mobiles);
			sbTemp.Append("&content=");
			sbTemp.Append(Content);

			byte[] bData = Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());

			System.Net.HttpWebRequest hwRequest;
			System.Net.HttpWebResponse hwResponse;

			string strResult = string.Empty;
			try
			{
				hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
				hwRequest.Timeout = 5000;
				hwRequest.Method = "POST";
				hwRequest.ContentType = "application/x-www-form-urlencoded";
				hwRequest.ContentLength = bData.Length;

				System.IO.Stream smWrite = hwRequest.GetRequestStream();
				smWrite.Write(bData, 0, bData.Length);
				smWrite.Close();

				hwResponse = (HttpWebResponse)hwRequest.GetResponse();
				StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
				strResult = srReader.ReadToEnd();
				srReader.Close();
				hwResponse.Close();

				rtn = (strResult == "100");
			}
			catch (Exception ex)
			{
				Apq.GlobalObject.ApqLog.Error("发送短信失败", ex);
			}
			return rtn;
		}
	}
}
